package com.bootdo.common.utils.share;


import com.bootdo.common.config.Constant;
import com.bootdo.common.utils.RedisUtil;
import redis.clients.jedis.Jedis;

public class uniteGetAccessToken {
        
//缓存acstoken
public static String getToken(String appid,String appsecret) {

	Jedis jedis = RedisUtil.getJedis();
    String acstoken = jedis.get(Constant.PUB_ACTOKEN_KEY);
    if (acstoken == null) {
    	String token = GetAccessTokenUtil.getAccessToken(appid,appsecret).get("accessToken");//向微信请求access_token
        jedis.set(Constant.PUB_ACTOKEN_KEY, token);
        return token;
    } else {
        String url = "https://api.weixin.qq.com/cgi-bin/getcallbackip?access_token=" + acstoken;
        if (isEffective(url)) {//判断redis中的token是否有效
        	return acstoken;
        } else {
        	String token = GetAccessTokenUtil.getAccessToken(appid,appsecret).get("accessToken");//向微信请求access_token
            jedis.set(Constant.PUB_ACTOKEN_KEY, token);
            return token;
        }
    }
    
    

}



static  Boolean isEffective(String url) {
    for (int i = 1; i <= 5; i++) {
        String result = HttpUtil.doGet(url);
        System.out.println(result);
        if (result.contains("ip_list")) {
            return Boolean.valueOf(true);
        }
    }
    return Boolean.valueOf(false);

}
	
	
}
